package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.CompanyInfoMapper;
import com.px.model.CompanyInfo;
import com.px.model.getData;
import com.px.unit.Response;

@Controller
public class CompanyInfoController {

	// 实例化我们的数据操作类

	CompanyInfoMapper company_mapper = new CompanyInfoMapper();

	/**
	 * 获取所有参选单位信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAllCompanyInfos", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getAllUsers() {
		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		List<CompanyInfo> companylist = company_mapper.getAllCompanyInfos();
		mydata.setCompanyInfoList(companylist);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 添加参选会员单位
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/insertCompanyInfos", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response insertCompanyInfos(@RequestBody CompanyInfo company) {

		if (company != null) {

			// 判断添加的参选会员单位不能重复
			int checkname = company_mapper.checkCompanyInfoIdIsExist(company);
			if (checkname > 0) {
				return new Response(304, "已存在同名参选会员单位");
			} else {
				int counts = company_mapper.insertCompanyInfos(company);
				if (counts > 0) {
					return new Response(200, "添加成功！");
				} else {
					return new Response(304, "添加失败！");
				}
			}
		}
		return new Response(304, "添加失败！");
	}

	/*
	 * 按照id查询参选会员单位信息
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getCompanyInfoByCompanyInfoId", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getCompanyInfoByCompanyInfoId(@RequestBody CompanyInfo company) {
		if (company != null) {
			// 判断查询的参选会员单位是否存在
			int checkid = company_mapper.checkCompanyInfoIdIsExist(company);
			if (!(checkid > 0)) {
				return new Response(304, "该参选会员单位不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<CompanyInfo> companyinfoList = company_mapper.getCompanyInfoByCompanyInfoId(company);
				mydata.setCompanyInfoList(companyinfoList);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

	/**
	 * 删除参选会员单位信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteCompanyInfoByCompanyInfoId", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteCompanyInfoByCompanyInfoId(@RequestBody CompanyInfo company) {

		if (company != null) {

			// 判断删除的参选会员单位是否存在
			int checkname = company_mapper.checkCompanyInfoIdIsExist(company);
			if (!(checkname > 0)) {
				return new Response(304, "该参选会员单位不存在");
			} else {
				int counts = company_mapper.deleteCompanyInfoByCompanyInfoId(company.getCompany_id());
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 修改参选会员单位信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateCompanyInfoByCompanyInfoId", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateCompanyInfoByCompanyInfoId(@RequestBody CompanyInfo company) {

		if (company != null) {

			// 判断修改的参选会员单位是否存在
			int checkname = company_mapper.checkCompanyInfoIdIsExist(company);
			if (!(checkname > 0)) {
				return new Response(304, "该参选会员单位不存在");
			} else {
				int counts = company_mapper.updateCompanyInfoByCompanyInfoId(company);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

}
